package tgc.edu.tms.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import tgc.edu.tms.custom.AjaxResult;
import tgc.edu.tms.custom.CommonController;
import tgc.edu.tms.custom.ExportExcel;
import tgc.edu.tms.custom.TablePageable;
import tgc.edu.tms.custom.TimeUtils;
import tgc.edu.tms.entity.ClassSchedule;
import tgc.edu.tms.entity.Classroom;
import tgc.edu.tms.entity.FlowTable;
import tgc.edu.tms.entity.Location;
import tgc.edu.tms.entity.Profession;
import tgc.edu.tms.entity.SysUser;
import tgc.edu.tms.entity.TbClass;
import tgc.edu.tms.entity.Teacher;
import tgc.edu.tms.entity.TtbPicture;
import tgc.edu.tms.entity.TtbRecord;
import tgc.edu.tms.security.UserUtils;
import tgc.edu.tms.service.ClassScheduleService;
import tgc.edu.tms.service.ClassroomService;
import tgc.edu.tms.service.FlowTableService;
import tgc.edu.tms.service.LocationService;
import tgc.edu.tms.service.ProfessionService;
import tgc.edu.tms.service.SysUserService;
import tgc.edu.tms.service.TbClassService;
import tgc.edu.tms.service.TeacherService;
import tgc.edu.tms.service.TtbPictureService;
import tgc.edu.tms.service.TtbRecordService;
import tgc.edu.tms.web.form.GRecordForm;

@Controller
@RequestMapping(value="/ttb")
public class TtbController extends CommonController<TtbRecord, Integer, GRecordForm> {
	//会话
	@Autowired
	private UserUtils userUtils;
	//课程
	@Autowired
	private ClassScheduleService classScheduleServie;
	//教师
	@Autowired
	private TeacherService teacherService;
	//记录表
	@Autowired
	private TtbRecordService ttbService;
	//记录图片表
	@Autowired
	private TtbPictureService ttbPictureService;
	@Autowired
	private ClassroomService classroomService;
	@Autowired
	private LocationService locationService;
	//流水表
	@Autowired
	private FlowTableService flowTableService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private ProfessionService professService;
	@Autowired
	private TbClassService tbClassService;
	
	/*
	 * 子首页面
	 */
	@RequestMapping("/ttb")

	public String ttb(){
		return "ttb/ttb";
	}
	@RequestMapping("/ttb1")
	public String ttb1(){
		return "ttb/ttb1";
	}
	/*
	 * 页面加载表格页面
	 */
	@RequestMapping("/g_give")
	public String give(String data, ModelMap map){
		SysUser user = userUtils.getUser();
		List<FlowTable> list = flowTableService.findByTimeAndTeacher(data, user.getName());
		map.put("list", list);
		return "ttb/g_give";
	}
	@RequestMapping("/g_give1")
	public String give1(String data, ModelMap map){
		SysUser user = userUtils.getUser();
		List<FlowTable> list = flowTableService.findByTimeAndTeacher(data, user.getName());
		map.put("list", list);
		return "ttb/g_give1";
	}
	
	/**
	 * 首页
	 * */
	@RequestMapping("/g_sy")
	public String g_sy(ModelMap map) {
		return "ttb/g_sy";
	}
	@RequestMapping("/g_sy1")
	public String g_sy1(ModelMap map) {
		return "ttb/g_sy1";
	}
	
	/*
	 * 调课模态框页面
	 */
	@RequestMapping("/g_changeCourse")
	public String changeCourse(Integer id, ModelMap map){
		/*ClassSchedule classSchedule = classScheduleServie.findById(id);
		map.put("model", classSchedule);
		*/
		List<Classroom> g_classroom = classroomService.findAll();
		List<Teacher> g_list1 = teacherService.findAll();
		FlowTable model = flowTableService.findById(id);
		map.put("g_list1", g_list1);
		map.put("model", model);
		map.put("g_classroom", g_classroom);
		return "ttb/g_changeCourse";
	}
	@RequestMapping("/g_changeCourse1")
	public String changeCourse1(Integer id, ModelMap map){
		/*ClassSchedule classSchedule = classScheduleServie.findById(id);
		map.put("model", classSchedule);
		 */
		List<Classroom> g_classroom = classroomService.findAll();
		List<Teacher> g_list1 = teacherService.findAll();
		FlowTable model = flowTableService.findById(id);
		map.put("g_list1", g_list1);
		map.put("model", model);
		map.put("g_classroom", g_classroom);
		return "ttb/g_changeCourse1";
	}
	
	/*
	 * 调课申请
	 */
	@RequestMapping("/g_tk_saveTeacherRecord")
	@ResponseBody
	public Object g_tk_saveRecord(GRecordForm form){
		try {
			SysUser user1 = userUtils.getUser();
			if(!(form.getTeacher().equals("--请选择老师--")||form.getTeacher().equals("")||form.getTeacher()==null)&&(form.getAddress().equals("--请选择地点--")||form.getAddress().equals("")||form.getAddress()==null)&&(form.getJc().equals("--请选择节次--")||form.getJc().equals("")||form.getJc()==null)){
				String section = form.getSection();
				String sect = section.substring(section.indexOf(",")+1);
				List<FlowTable> flow = flowTableService.findByTimeAndJcAndTeacher(form.getSuspendTime(),sect,form.getTeacher());
				if(flow.size()!=0){
					return new AjaxResult(false, "调课有冲突");
				}
				TtbRecord record = new TtbRecord();
				if(form.getId() == null) {
					TtbRecord ttbRecord = ttbService.findByUserAndSuspendTimeAndSection(user1, form.getSuspendTime(), form.getSection());
					if(ttbRecord == null) {
						SysUser user = userUtils.getUser();
						form.setUser(user);
						Teacher t = teacherService.findByName(form.getTeacher());
						form.setTeacher(t.getName());
						form.setSection(section.substring(section.indexOf(",")+1));
						form.setTypeTwo("调老师");
						form.setType("调课");
						form.setState("申请中");
						form.setLeaveType("其他");
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
						form.setOpplyTime(df.format(new Date()));
						form.setAddress("-");
						form.setJc("-");
						form.setReason("-");
						BeanUtils.copyProperties(form, record);
						ttbService.save(record);
						System.out.println("只换老师");	
					}else {
						return new AjaxResult("此课程您已申请过！请不要重复申请！");
					}
				}else {
					TtbRecord record1 = ttbService.findById(form.getId());
					record1.setTeacher(form.getTeacher());
					record1.setState("申请中");
					ttbService.save(record1);
					System.out.println("再次申请！");
				}
			}else if(!(form.getTeacher().equals("--请选择老师--")||form.getTeacher().equals("")||form.getTeacher()==null)&&!(form.getAddress().equals("--请选择地点--")||form.getAddress().equals("")||form.getAddress()==null)&&(form.getJc().equals("--请选择节次--")||form.getJc().equals("")||form.getJc()==null)){
				String section = form.getSection();
				String sect = section.substring(section.indexOf(",")+1);
				List<FlowTable> flow1 = flowTableService.findByTimeAndJcAndTeacher(form.getSuspendTime(),sect,form.getTeacher());
				List<FlowTable> flow2 = flowTableService.findByTimeAndJcAndClassRoom(form.getSuspendTime(), section, form.getAddress());
				if(flow1.size()!=0||flow2.size()!=0){
					return new AjaxResult(false, "调课有冲突！");
				}
				if(form.getId() == null) {
					TtbRecord ttbRecord = ttbService.findByUserAndSuspendTimeAndSection(user1, form.getSuspendTime(), form.getSection());
					if(ttbRecord == null) {
						TtbRecord record = new TtbRecord();
						SysUser user = userUtils.getUser();
						form.setUser(user);
						Teacher t = teacherService.findByName(form.getTeacher());
						form.setTeacher(t.getName());
						form.setSection(section.substring(section.indexOf(",")+1));
						form.setTypeTwo("调老师和地点");
						form.setType("调课");
						form.setState("申请中");
						form.setLeaveType("其他");
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
						form.setOpplyTime(df.format(new Date()));
						form.setJc("-");
						form.setReason("-");
						String classroom = form.getClassroom();
						form.setClassroom(classroom.substring(classroom.indexOf(",")+1));
						BeanUtils.copyProperties(form, record);
						ttbService.save(record);		
					}else {
						return new AjaxResult("此课程您已申请过！请不要重复申请！");
					}
				}else {
					TtbRecord record1 = ttbService.findById(form.getId());
					record1.setTeacher(form.getTeacher());
					record1.setAddress(form.getAddress());
					record1.setState("申请中");
					ttbService.save(record1);
					System.out.println("再次申请！");
				}
			}else if(!(form.getTeacher().equals("--请选择老师--")||form.getTeacher().equals("")||form.getTeacher()==null)&&!(form.getAddress().equals("--请选择地点--")||form.getAddress().equals("")||form.getAddress()==null)&&!(form.getJc().equals("--请选择节次--")||form.getJc().equals("")||form.getJc()==null)){
				SysUser user = userUtils.getUser();
				String section = form.getSection();
				String sect = section.substring(section.indexOf(",")+1);
				List<FlowTable> flow6 = flowTableService.findByTimeAndJcAndTeacher(form.getRepairTime(), form.getJc(),form.getTeacher());
				List<FlowTable> flow5 = flowTableService.findByTimeAndJcAndClassRoom(form.getRepairTime(), form.getJc(), form.getAddress());
				List<FlowTable> flow1 = flowTableService.findByTimeAndJcAndTeacher(form.getSuspendTime(),sect,form.getTeacher());
				List<FlowTable> flow2 = flowTableService.findByTimeAndJcAndClassRoom(form.getSuspendTime(), section, form.getAddress());
				
				if(!(user.getName()).equals(form.getTeacher())){
					if(flow6.size()!=0){
						System.out.println("++++");
						return new AjaxResult(false, "调课有冲突！");
					}
				}
				if(!(form.getClassroom()).equals(form.getAddress())){
					for(FlowTable f: flow5){
						System.out.println(f.getId()+"===");
					}
					if(flow5.size()!=0){
						System.out.println("------");
						return new AjaxResult(false, "调课有冲突！");
					}
				}
				System.out.println("调课前时间："+form.getSuspendTime()+"调课后的时间："+form.getRepairTime()+form.getJc()+form.getTbClass());
				System.out.println("===="+form.getYear());
				if(!(form.getSuspendTime()).equals(form.getRepairTime())){
					FlowTable flow3 = flowTableService.findByTimeAndJcAndClassesAndYear(form.getRepairTime(),form.getJc(),form.getTbClass(),form.getYear());
					if(flow3!=null){
						System.out.println("调课2");
						return new AjaxResult(false, "调课有冲突！");
					}
					FlowTable flow4 = flowTableService.findByJcAndTimeAndTeacher(form.getJc(),form.getRepairTime(),form.getTeacher());
					if(flow4!=null){
						System.out.println("调课3");
						return new AjaxResult(false, "调课有冲突！");
					}
				}
				if(form.getId()==null) {
					TtbRecord ttbRecord = ttbService.findByUserAndSuspendTimeAndSection(user1, form.getSuspendTime(), form.getSection());
					if(ttbRecord == null) {
						TtbRecord record = new TtbRecord();
						form.setUser(user);
						Teacher t = teacherService.findByName(form.getTeacher());
						form.setTeacher(t.getName());
						form.setSection(section.substring(section.indexOf(",")+1));
						form.setTypeTwo("调老师地点时间");
						form.setType("调课");
						form.setState("申请中");
						form.setLeaveType("其他");
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
						form.setOpplyTime(df.format(new Date()));
						form.setReason("-");
						BeanUtils.copyProperties(form, record);
						ttbService.save(record);
					}else {
						return new AjaxResult("此课程您已申请过！请不要重复申请！");
					}
				}else {
					TtbRecord recorrd1 = ttbService.findById(form.getId());
					recorrd1.setTeacher(form.getTeacher());
					recorrd1.setAddress(form.getAddress());
					String repairTime = form.getRepairTime();
					recorrd1.setRepairTime(repairTime);
					recorrd1.setWeek(TimeUtils.WeekDay(repairTime));
					recorrd1.setJc(form.getJc());
					recorrd1.setState("申请中");
					ttbService.save(recorrd1);
				}
			}else if((form.getTeacher().equals("--请选择老师--")||form.getTeacher().equals("")||form.getTeacher()==null)&&!(form.getAddress().equals("--请选择地点--")||form.getAddress().equals("")||form.getAddress()==null)&&(form.getJc().equals("--请选择节次--")||form.getJc().equals("")||form.getJc()==null)){
				List<FlowTable> flow = flowTableService.findByTimeAndJcAndClassRoom(form.getSuspendTime(), form.getSection(), form.getAddress());
				if(flow.size()!=0){
					return new AjaxResult(false, "调课有冲突！");
				}
				if(form.getId() == null) {
					TtbRecord ttbRecord = ttbService.findByUserAndSuspendTimeAndSection(user1, form.getSuspendTime(), form.getSection());
					if(ttbRecord == null) {
						SysUser user = userUtils.getUser();
						TtbRecord record = new TtbRecord();
						form.setUser(user);
						form.setTypeTwo("调地点");
						form.setType("调课");
						form.setState("申请中");
						form.setLeaveType("其他");
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
						form.setOpplyTime( df.format(new Date()));
						form.setTeacher("-");
						form.setJc("-");
						form.setReason("-");
						String classroom = form.getClassroom();
						form.setClassroom(classroom.substring(classroom.indexOf(",")+1));
						BeanUtils.copyProperties(form, record);
						ttbService.save(record);
					}else {
						return new AjaxResult("此课程您已申请过！请不要重复申请！");
					}
				}else {
					TtbRecord record1 = ttbService.findById(form.getId());
					record1.setAddress(form.getAddress());
					record1.setState("申请中");
					ttbService.save(record1);
				}
				
			}else if((form.getTeacher().equals("--请选择老师--")||form.getTeacher().equals("")||form.getTeacher()==null)&&!(form.getAddress().equals("--请选择地点--")||form.getAddress().equals("")||form.getAddress()==null)&&!(form.getJc().equals("--请选择节次--")||form.getJc().equals("")||form.getJc()==null)){
				SysUser user = userUtils.getUser();
				List<FlowTable> flow2 = flowTableService.findByTimeAndJcAndClassRoom(form.getSuspendTime(), form.getSection(), form.getAddress());
				if(flow2.size()!=0){
					return new AjaxResult(false, "调课有冲突！");
				}
				FlowTable flow3 = flowTableService.findByTimeAndJcAndClassesAndYear(form.getRepairTime(),form.getJc(),form.getTbClass(),form.getYear());
				if(flow3!=null){
					return new AjaxResult(false, "调课有冲突！");
				}
				String teaname = user.getName();
				FlowTable flow4 = flowTableService.findByJcAndTimeAndTeacher(form.getJc(),form.getRepairTime(),teaname);
				if(flow4!=null){
					return new AjaxResult(false, "调课有冲突！");
				}
				if(form.getId() == null){
					TtbRecord ttbRecord = ttbService.findByUserAndSuspendTimeAndSection(user1, form.getSuspendTime(), form.getSection());
					if(ttbRecord == null) {
						TtbRecord record = new TtbRecord();
						form.setUser(user);
						form.setTypeTwo("调地点和时间");
						form.setType("调课");
						form.setState("申请中");
						form.setLeaveType("其他");
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
						form.setOpplyTime( df.format(new Date()));
						form.setTeacher("-");
						form.setReason("-");
						String classroom = form.getClassroom();
						form.setClassroom(classroom.substring(classroom.indexOf(",")+1));
						BeanUtils.copyProperties(form, record);
						ttbService.save(record);
					}else {
						return new AjaxResult("此课程您已申请过！请不要重复申请！");
					}
				}else {
					TtbRecord record1 = ttbService.findById(form.getId());
					record1.setAddress(form.getAddress());
					String repairTime = form.getRepairTime();
					record1.setRepairTime(repairTime);
					record1.setWeek(TimeUtils.WeekDay(repairTime));
					record1.setJc(form.getJc());
					record1.setState("申请中");
					ttbService.save(record1);
				}
				
			}else if((form.getTeacher().equals("--请选择老师--")||form.getTeacher().equals("")||form.getTeacher()==null)&&(form.getAddress().equals("--请选择地点--")||form.getAddress().equals("")||form.getAddress()==null)&&!(form.getJc().equals("--请选择节次--")||form.getJc().equals("")||form.getJc()==null)){
				SysUser user = userUtils.getUser();
				FlowTable flow = flowTableService.findByTimeAndJcAndClassesAndYear(form.getRepairTime(),form.getJc(),form.getTbClass(),form.getYear());
				if(flow!=null){
					return new AjaxResult(false, "调课有冲突！");
				}
				String teaname = user.getName();
				FlowTable flow2 = flowTableService.findByJcAndTimeAndTeacher(form.getJc(),form.getRepairTime(),teaname);
				if(flow2!=null){
					return new AjaxResult(false, "调课有冲突！");
				}
				if(form.getId() == null) {
					TtbRecord ttbRecord = ttbService.findByUserAndSuspendTimeAndSection(user1, form.getSuspendTime(), form.getSection());
					if(ttbRecord == null) {
						TtbRecord record = new TtbRecord();
						form.setUser(user);
						form.setTypeTwo("调时间");
						form.setType("调课");
						form.setState("申请中");
						form.setLeaveType("其他");
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
						form.setOpplyTime(df.format(new Date()));
						form.setTeacher("-");
						form.setAddress("-");
						form.setReason("-");
						String section = form.getSection();
						form.setSection(section.substring(section.indexOf(",")+1));
						BeanUtils.copyProperties(form, record);
						ttbService.save(record);
					}else {
						return new AjaxResult("此课程您已申请过！请不要重复申请！");
					}
				}else {
					TtbRecord record1 = ttbService.findById(form.getId());
					String repairTime = form.getRepairTime();
					record1.setRepairTime(repairTime);
					record1.setWeek(TimeUtils.WeekDay(repairTime));
					record1.setJc(form.getJc());
					record1.setState("申请中");
					ttbService.save(record1);
				}
			}else if(!(form.getTeacher().equals("--请选择老师--")||form.getTeacher().equals("")||form.getTeacher()==null)&&(form.getAddress().equals("--请选择地点--")||form.getAddress().equals("")||form.getAddress()==null)&&!(form.getJc().equals("--请选择节次--")||form.getJc().equals("")||form.getJc()==null)){
				List<FlowTable> flow = flowTableService.findByTimeAndJcAndTeacher(form.getSuspendTime(),form.getSection(),form.getTeacher());
				if(flow.size()!=0){
					return new AjaxResult(false, "调课有冲突");
				}
				FlowTable flow1 = flowTableService.findByTimeAndJcAndClassesAndYear(form.getRepairTime(),form.getJc(),form.getTbClass(),form.getYear());
				if(flow1!=null){
					return new AjaxResult(false, "调课有冲突！");
				}
//				String teaname = user.getName();
//				System.out.println("+++++"+teaname);
				FlowTable flow2 = flowTableService.findByJcAndTimeAndTeacher(form.getJc(),form.getRepairTime(),form.getTeacher());
				if(flow2!=null){
					return new AjaxResult(false, "调课有冲突！");
				}
				if(form.getId() == null) {
					TtbRecord ttbRecord = ttbService.findByUserAndSuspendTimeAndSection(user1, form.getSuspendTime(), form.getSection());
					if(ttbRecord == null) {
						TtbRecord record = new TtbRecord();
						SysUser user = userUtils.getUser();
						form.setUser(user);
						form.setTypeTwo("调老师和时间");
						form.setType("调课");
						form.setState("申请中");
						form.setLeaveType("其他");
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
						form.setOpplyTime(df.format(new Date()));
						form.setAddress("-");
						form.setReason("-");
						String section = form.getSection();
						form.setSection(section.substring(section.indexOf(",")+1));
						BeanUtils.copyProperties(form, record);
						ttbService.save(record);
					}else {
						return new AjaxResult("此课程您已申请过！请不要重复申请！");
					}
				}else {
					TtbRecord record1 = ttbService.findById(form.getId());
					record1.setTeacher(form.getTeacher());
					String repairTime = form.getRepairTime();
					record1.setRepairTime(repairTime);
					record1.setWeek(TimeUtils.WeekDay(repairTime));
					record1.setJc(form.getJc());
					record1.setState("申请中");
					ttbService.save(record1);
				}
			}
			return new AjaxResult("申请成功");
		  } catch (Exception e) { 
			  return new AjaxResult(false, "申请失败"); 
		  }
		 
	
		
	}
	
	/*
	 * 停课模态框页面
	 * （向忠义）
	 */
	@RequestMapping("/g_suspendCourse")
	public String suspendCourse(Integer id, ModelMap map){
		FlowTable flowTable = flowTableService.findById(id);
		List<Teacher> g_list1 = teacherService.findAll();
		map.put("g_list1", g_list1);
		map.put("model", flowTable);
		return "ttb/g_suspendCourse";
	}
	@RequestMapping("/g_suspendCourse1")
	public String suspendCourse1(Integer id, ModelMap map){
		FlowTable flowTable = flowTableService.findById(id);
		List<Teacher> g_list1 = teacherService.findAll();
		map.put("g_list1", g_list1);
		map.put("model", flowTable);
		return "ttb/g_suspendCourse1";
	}
	
	
	/*
	 * 停课申请
	 */
	@RequestMapping("/f_tk_saveRecord")
	@ResponseBody
	public Object f_tk_saveRecord(GRecordForm form,String picture) {
		try {
			SysUser user = userUtils.getUser();
			if(form.getId() == null) {
				TtbRecord ttbRecord = ttbService.findByUserAndSuspendTimeAndSection(user, form.getSuspendTime(), form.getSection());
				if(ttbRecord == null) {
					TtbRecord record = new TtbRecord();
					form.setUser(user);
					form.setTeacher(user.getName());
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
					String date = df.format(new Date());
					form.setOpplyTime(date);
					form.setState("申请中");
					form.setType("停课");
					form.setTypeTwo("已停课");
					BeanUtils.copyProperties(form, record);
					ttbService.save(record);
					Sort sort = new Sort(Direction.DESC, "id");
					List<TtbRecord> list = ttbService.findAll(sort);
					TtbRecord record1 = list.get(0);
					String[] id = picture.split(",");   //拆分字符串，
					for(int i = 0; i < id.length; i++) {
						TtbPicture ttbPicture = ttbPictureService.findById(Integer.parseInt(id[i]));
						ttbPicture.setRecord(record1);				
						ttbPictureService.save(ttbPicture);
					}
					return new AjaxResult("申请成功！");
				}else {
					return new AjaxResult("此课程您已申请过！请不要重复申请！");
				}
			}else {
				TtbRecord record = ttbService.findById(form.getId());
				record.setLeaveType(form.getLeaveType());
				record.setReason(form.getReason());
				record.setState("申请中");
				ttbService.save(record);
				String[] id = picture.split(",");   //拆分字符串，
				for(int i = 0; i < id.length; i++) {
					TtbPicture ttbPicture = ttbPictureService.findById(Integer.parseInt(id[i]));
					ttbPicture.setRecord(record);				
					ttbPictureService.save(ttbPicture);
				}
				return new AjaxResult("申请成功！");
			}
		} catch (Exception e) {
			return new AjaxResult(false, "申请失败");
		}
	}
	/**
	 * 图片删除
	 * */
	@RequestMapping("/picture_remove")
	@ResponseBody
	public Object picture_remove(Integer id) {
		ttbPictureService.deleteById(id);
		return new AjaxResult("删除成功！");	
	}
	
	
	@RequestMapping("/g_lctk")
	public String g_lctk(ModelMap map){
		List<Location> f_loca = locationService.findAll();
		map.put("f_loca", f_loca);
		return "ttb/g_lctk";
	}
	@RequestMapping("/f_findfloor")
	public Object f_findfloor(String name,ModelMap map) {
		Location id = locationService.findByName(name);
		List<String> classroom =  classroomService.findByLocationId(id.getId());
		map.put("classroom", classroom);
		return "ttb/g_change";
	}
	/**
	 * 楼层停课
	 * */
	@RequestMapping("/f_saveRecord")
	@ResponseBody
	public Object f_saveRecord(String time,String ld,String lc) {
		try {
			List<FlowTable> cr = flowTableService.findByTimeAndLdAndLc(time,ld,lc);
			if(cr.size()==0) {
				return new AjaxResult(false, "此楼层在该日期没有教室有课，可重新选择！");
			}else {
				for (FlowTable ss : cr) {
					TtbRecord record = new TtbRecord();
					record.setCurriculum(ss.getCourse());
					record.setClassroom(ss.getClassRoom());
					record.setSection(ss.getJc());
					record.setSuspendTime(time);
					record.setTbClass(ss.getClasses());
					record.setKclbs(ss.getKclbs());
					record.setType("停课");
					record.setTypeTwo("已停课");
					record.setState("已同意");
					Teacher teacher = teacherService.findByName(ss.getTeacher());
					record.setUser(teacher);
					record.setTeacher(ss.getTeacher());
					record.setZxs(ss.getZxs());
					record.setYdrs(ss.getYdrs());
					record.setLc(ss.getLc());
					record.setLd(ss.getLd());
					record.setYear(ss.getYear());
					record.setTerm(ss.getTerm());
					record.setXmh(ss.getXmh());
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
					String date = df.format(new Date());
					record.setOpplyTime(date);
					ttbService.save(record);
					flowTableService.delete(ss);
				}
			}
			return new AjaxResult("停课成功！");
		} catch (Exception e) {
			return new AjaxResult(false, "停课失败！");
		}
	}

	/**
	 * 上传图片
	 * 
	 */
	@RequestMapping("/f_tk_savePicture")
	@ResponseBody
	public Object f_tk_savePicture(MultipartFile file) {
		ttbPictureService.save(file);
		Sort sort = new Sort(Direction.DESC, "id");
		List<TtbPicture> list = ttbPictureService.findAll(sort);
		TtbPicture picture = list.get(0);
		String res = String.valueOf(picture.getId());
		return new AjaxResult(true, res);
	}
	/**
	 * 下载
	 * */
	@RequestMapping(value="/download")
	@ResponseBody
	public Object download(Integer id, HttpServletRequest request, HttpServletResponse response) {
		ttbPictureService.download(id, request, response);
		return null;
	}
	/*
	 * 补课模态框
	 * （夏雨晨）
	 */
	@RequestMapping("/g_tutorialCourse")
	public String tutorialCourse(ModelMap map){
		return "ttb/g_tutorialCourse";
	}
	@RequestMapping("/g_tutorialCourse1")
	public String tutorialCourse1(ModelMap map){
		return "ttb/g_tutorialCourse1";
	}
	/*
	 * 补课申请页面
	 */
	@RequestMapping("/g_tksq")
	public String g_tksq(ModelMap map){
		SysUser user = userUtils.getUser();
		List<TtbRecord> list = ttbService.findByUserAndTypeTwoAndState(user,"已停课","已同意");
		map.put("list", list);
		return "ttb/g_tksq";
	}
	@RequestMapping("/g_tksq1")
	public String g_tksq1(ModelMap map){
		SysUser user = userUtils.getUser();
		List<TtbRecord> list = ttbService.findByUserAndTypeTwoAndState(user,"已停课","已同意");
		map.put("list", list);
		return "ttb/g_tksq1";
	}
	/*
	 * 补课信息
	 */
	@RequestMapping("/g_bk_message")
	public String bk_message(Integer id, ModelMap map){
		TtbRecord ttbRecord = ttbService.findById(id);
		List<Teacher> list = teacherService.findAll();
		List<Classroom> list1 = classroomService.findAll();
		map.put("teacherroom", list);
		map.put("classroomlist", list1);
		map.put("record", ttbRecord);
		map.put("id", id);
		return "ttb/g_bk_message";
	}
	@RequestMapping("/g_bk_message1")
	public String bk_message1(Integer id, ModelMap map){
		TtbRecord ttbRecord = ttbService.findById(id);
		List<Teacher> list = teacherService.findAll();
		List<Classroom> list1 = classroomService.findAll();
		map.put("teacherroom", list);
		map.put("classroomlist", list1);
		map.put("record", ttbRecord);
		map.put("id", id);
		return "ttb/g_bk_message1";
	}
	/*
	 * 补课申请
	 */
	@RequestMapping("/i_tjbksq")
	@ResponseBody
	public Object i_tjbksq(GRecordForm form) {
		try {
			SysUser user = userUtils.getUser();
			
			//String weekDay = TimeUtils.WeekDay(form.getRepairTime());//补课日期转换星期
			SysUser teacher = sysUserService.findByUsername(form.getTeacher());//查出补课老师的姓名
			FlowTable flowTable = flowTableService.findByJcAndTimeAndClassRoom(form.getJc(),form.getRepairTime(),form.getAddress());//查找这个教室这个时间有没有课
			FlowTable flowTable2 = flowTableService.findByJcAndTimeAndTeacher(form.getJc(),form.getRepairTime(),teacher.getName());//查找这个时间这个上课老师又没有课
			FlowTable flowTable3 = flowTableService.findByJcAndTimeAndClassesAndYear(form.getJc(),form.getRepairTime(),form.getTbClass(),form.getYear());//查找这个时间这个补课班级有没有课
			if(flowTable==null) {
				if(flowTable2==null) {
					if(flowTable3==null) {
						if(form.getChange() == null) {
							TtbRecord ttbRecord = ttbService.findByUserAndSuspendTimeAndSectionAndTypeTwoIsNull(user, form.getSuspendTime(), form.getSection());
							if(ttbRecord == null) {
								SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
								String date = df.format(new Date());
								TtbRecord record = ttbService.findById(form.getId());
								TtbRecord rc = new TtbRecord();
								rc.setReason(form.getReason());
								rc.setUser(record.getUser());
								rc.setKclbs(record.getKclbs());
								rc.setType("补课");
								rc.setState("申请中");
								rc.setOpplyTime(date);
								rc.setRepairTime(form.getRepairTime());
								rc.setClassroom(form.getClassroom());
								String tbClass = form.getTbClass();
								rc.setTbClass(tbClass.substring(tbClass.indexOf(",")+1));
								String curriculum = form.getCurriculum();
								rc.setCurriculum(curriculum.substring(curriculum.indexOf(",")+1));
								rc.setSection(form.getSection());
								rc.setAddress(form.getAddress());
								rc.setJc(form.getJc());
								rc.setSuspendTime(form.getSuspendTime());
								rc.setSection(form.getSection());
								rc.setTeacher(teacher.getName());
								rc.setYdrs(form.getYdrs());
								rc.setZxs(form.getZxs());
								rc.setLd(form.getLd());
								rc.setLc(form.getLc());
								rc.setReason(form.getReason());
								rc.setClassroom(form.getClassroom());
								//单纯存个星期几，用户体验好
								rc.setWeek(TimeUtils.WeekDay(form.getRepairTime()));
								rc.setYear(form.getYear());
								ttbService.save(rc);
								return "补课申请成功，请等待管理员审核!";
							}else {
								return "您已申请过！请不要重复申请！";
							}
						}else {
							TtbRecord record = ttbService.findById(form.getChange());
							record.setJc(form.getJc());
							record.setRepairTime(form.getRepairTime());
							record.setTeacher(form.getTeacher());
							record.setAddress(form.getAddress());
							String weekDay = TimeUtils.WeekDay(form.getRepairTime());
							record.setWeek(weekDay);
							record.setState("申请中");
							ttbService.save(record);
							return "申请成功！";
						}
					}else {
						return "这个时间您想补课的班级有课哦~ 请换个时间再补吧！";
					}
				}else {
					return "这个时间您选择的授课老师有课哦~ 请您协商换个时间吧！";
				}
			}else {
				return "这个时间教室有课哦~ 请您换个时间吧！";
			}
		} catch (Exception e) {
			return "网络或数据存在异常！";
		}
	}
	/*
	 * 页面加载记录表页面
	 */
	@RequestMapping("/g_apply")
	public String apply(ModelMap map){
		SysUser user = userUtils.getUser();
		Sort sort = new Sort(Direction.DESC, "id");
		List<TtbRecord> ttbRecord = ttbService.findByUserId(user.getId(), sort);
		map.put("j_ttbRecord", ttbRecord);
		return "ttb/g_apply";
	}
	@RequestMapping("/g_apply1")
	public String apply1(ModelMap map){
		SysUser user = userUtils.getUser();
		Sort sort = new Sort(Direction.DESC, "id");
		List<TtbRecord> ttbRecord = ttbService.findByUserId(user.getId(), sort);
		map.put("j_ttbRecord", ttbRecord);
		return "ttb/g_apply1";
	}
	/*
	 * 记录表详情页面
	 */
	@RequestMapping("/g_particulars")
	public String g_particulars(Integer id, ModelMap map){
		TtbRecord record = ttbService.findById(id);
		List<TtbPicture> picture = ttbPictureService.findByRecordId(id);
		List<Teacher> list = teacherService.findAll();
		List<Classroom> list1 = classroomService.findAll();
		map.put("teacherroom", list);
		map.put("classroomlist", list1);
		map.put("record", record);
		map.put("picture", picture);
		return "ttb/g_particulars";
	}
	@RequestMapping("/g_particulars1")
	public String g_particulars1(Integer id, ModelMap map){
		TtbRecord record = ttbService.findById(id);
		List<TtbPicture> picture = ttbPictureService.findByRecordId(id);
		List<Teacher> list = teacherService.findAll();
		List<Classroom> list1 = classroomService.findAll();
		map.put("teacherroom", list);
		map.put("classroomlist", list1);
		map.put("record", record);
		map.put("picture", picture);
		return "ttb/g_particulars1";
	}
	/*
	 * table详细信息
	 */
	@RequestMapping("/g_details")
	public String g_etails(Integer id, ModelMap map){
		ClassSchedule schedule = classScheduleServie.findById(id);
		map.put("schedule", schedule);
		return "ttb/g_details";
	} 
	/**
	 *  撤销申请
	 * */
	@RequestMapping("/j_cxsq")
	@ResponseBody
	public Object j_cxsq(Integer id) {
		ttbService.deleteById(id);
		return new AjaxResult("撤销成功！");
	}
	
	/**
	 * 
	 * 管理员审核
	 */
	@RequestMapping("/g_adminTtb")
	public String g_adminTtb(){
		return "ttb/g_adminTtb";
	}
	@RequestMapping("/g_adminTtb1")
	public String g_adminTtb1(ModelMap map){
		List<TtbRecord> g_change = ttbService.findByTypeAndState("调课","申请中");
		map.put("g_change", g_change);
		return "ttb/g_adminTtb1";
	} 
	@RequestMapping("/agreeController")
	@ResponseBody
	public Object agreeController(Integer id ,Integer a,String result){
		try {
			TtbRecord recoreId = ttbService.findById(id);
			FlowTable flow = flowTableService.findByTimeAndJcAndClassesAndYear(recoreId.getSuspendTime(),recoreId.getSection(), recoreId.getTbClass(),recoreId.getYear());
			List<FlowTable> flow1 = flowTableService.findByTimeAndJcAndClassRoom(recoreId.getRepairTime(),recoreId.getJc(),recoreId.getAddress());
			if(a==1){
				if(flow1.size() != 0){
					return new AjaxResult(false, "同意地点有冲突！请拒绝");
				}
				if(recoreId.getTypeTwo().equals("调老师")){
					String teacher = recoreId.getTeacher();
					flow.setTeacher(teacher);
					flowTableService.save(flow);
				}else if(recoreId.getTypeTwo().equals("调老师和地点")){
					String teacher = recoreId.getTeacher();
					flow.setTeacher(teacher);
					flow.setClassRoom(recoreId.getAddress());
					flowTableService.save(flow);
				}else if(recoreId.getTypeTwo().equals("调老师地点时间")){
					String repairTime = recoreId.getRepairTime();
					String weekDay = TimeUtils.WeekDay(repairTime);
					String teacher = recoreId.getTeacher();
					flow.setTeacher(teacher);
					flow.setClassRoom(recoreId.getAddress());
					flow.setTime(repairTime);
					flow.setWeek(weekDay);
					flow.setJc(recoreId.getJc());
					flowTableService.save(flow);
				}else if(recoreId.getTypeTwo().equals("调地点")){
					flow.setClassRoom(recoreId.getAddress());
					flowTableService.save(flow);
				}else if(recoreId.getTypeTwo().equals("调地点和时间")){
					String repairTime = recoreId.getRepairTime();
					String weekDay = TimeUtils.WeekDay(repairTime);
					flow.setClassRoom(recoreId.getAddress());
					flow.setTime(repairTime);
					flow.setWeek(weekDay);
					flow.setJc(recoreId.getJc());
					flowTableService.save(flow);
				}else if(recoreId.getTypeTwo().equals("调时间")){
					String repairTime = recoreId.getRepairTime();
					String weekDay = TimeUtils.WeekDay(repairTime);
					flow.setTime(repairTime);
					flow.setWeek(weekDay);
					flow.setJc(recoreId.getJc());
				}else if(recoreId.getTypeTwo().equals("调老师和时间")){
					String repairTime = recoreId.getRepairTime();
					String weekDay = TimeUtils.WeekDay(repairTime);
					String teacher = recoreId.getTeacher();
					flow.setTeacher(teacher);
					flow.setTime(repairTime);
					flow.setWeek(weekDay);
					flow.setJc(recoreId.getJc());
					flowTableService.save(flow);
				}
				recoreId.setState("已同意");
				ttbService.save(recoreId);
				return new AjaxResult("已同意！");
			}else{
				recoreId.setOpinion(result);
				ttbService.save(recoreId);
				recoreId.setState("已拒绝");
				ttbService.save(recoreId);
				return new AjaxResult("已拒绝！");
			}
		} catch (Exception e) {
			return new AjaxResult(false, "网络或数据存在异常！");
		}
	}
	
	//停课审核同意
	@RequestMapping("/g_adminTtb2")
	public String g_adminTtb2(ModelMap map){
		List<TtbRecord> g_tk = ttbService.findByTypeAndState("停课","申请中");
		map.put("g_tk", g_tk);
		return "ttb/g_adminTtb2";
	}
	@RequestMapping("/tkController")
	@ResponseBody
	public Object tkController(Integer id ,Integer a,String result){
		TtbRecord recoreId = ttbService.findById(id);
		if(a==1){
			FlowTable flow=flowTableService.findByTimeAndJcAndClassesAndYear(recoreId.getSuspendTime(),recoreId.getSection(), recoreId.getTbClass(),recoreId.getYear());
			flowTableService.delete(flow);
			recoreId.setState("已同意");
			ttbService.save(recoreId);
			return new AjaxResult("已同意！");
		}else{
			recoreId.setOpinion(result);
			ttbService.save(recoreId);
			recoreId.setState("已拒绝");
			ttbService.save(recoreId);
			return new AjaxResult("已拒绝！");
		}
	}
	@RequestMapping("/g_picture")
	public String g_picture(Integer id, ModelMap map){
		List<TtbPicture> recordId = ttbPictureService.findByRecordId(id);
		map.put("picture", recordId);
		return "ttb/g_picture";
	}
	
	//补课同意
	@RequestMapping("/g_adminTtb3")
	public String g_adminTtb3(ModelMap map){
		List<TtbRecord> g_bk = ttbService.findByTypeAndState("补课","申请中");
		map.put("g_bk", g_bk);
		return "ttb/g_adminTtb3";
	}
	@RequestMapping("/bkController")
	@ResponseBody
	public Object bkController(Integer id,Integer a,String result){
		TtbRecord bkid = ttbService.findById(id);
		String idstr = bkid.getReason();
		TtbRecord tkid = ttbService.findById(Integer.parseInt(idstr));
		String weekDay = TimeUtils.WeekDay(bkid.getRepairTime());//补课日期转换星期
		List<FlowTable> flow1 = flowTableService.findByTimeAndJcAndClassRoom(bkid.getRepairTime(),bkid.getJc(),bkid.getAddress());
		try {
			if(a==1){
				if(flow1.size() != 0){
					return new AjaxResult(false, "同意地点有冲突！请拒绝");
				}
				bkid.setState("已同意");
				tkid.setTypeTwo("已补课");
				ttbService.save(tkid);
				ttbService.save(bkid);
				FlowTable t = new FlowTable();
				t.setClasses(bkid.getTbClass());
				t.setCourse(bkid.getCurriculum());
				t.setClassRoom(bkid.getAddress());
				t.setTeacher(bkid.getTeacher());
				t.setZxs(bkid.getZxs());
				t.setTime(bkid.getRepairTime());
				t.setWeek(weekDay);
				t.setJc(bkid.getJc());
				t.setKclbs(bkid.getKclbs());
				t.setYdrs(bkid.getYdrs());
				t.setLd(bkid.getLc());
				t.setLc(bkid.getLd());
				t.setTerm(tkid.getTerm());
				t.setYear(tkid.getYear());
				t.setXmh(tkid.getXmh());
				flowTableService.save(t);
				return new AjaxResult("已同意！");
			}else{
				bkid.setOpinion(result);
				ttbService.save(bkid);
				bkid.setState("已拒绝");
				ttbService.save(bkid);
				return new AjaxResult("已拒绝！");
			}
		} catch (Exception e) {
			return new AjaxResult("网络或数据存在异常！");
		}
	}
	/**
	 * 调停补记录信息
	 */
	@RequestMapping("/ttbRecord")
	public String ttbRecord(){
		return "ttb/g_ttbRecord";
	}
	/**
	 * 调停补记录查询
	 */
	
	@Override
	public Specification<TtbRecord> buildSpec(GRecordForm form) {
		Specification<TtbRecord> spec = new Specification<TtbRecord>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<TtbRecord> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(StringUtils.hasText(form.getName())) {
					rules.add(cb.like(root.get("user").get("name"), "%"+form.getName()+"%"));
				}
				if(StringUtils.hasText(form.getType())){
					rules.add(cb.like(root.get("type"), "%"+form.getType()+"%"));
				}
				if(StringUtils.hasText(form.getState())){
					rules.add(cb.like(root.get("state"), "%"+form.getState()+"%"));
				}
				if(StringUtils.hasText(form.getSearch())){
					Predicate user = cb.like(root.get("user").get("name"), "%"+form.getSearch()+"%");
					Predicate state = cb.like(root.get("state"), "%"+form.getSearch()+"%");
					Predicate type = cb.like(root.get("type"), "%"+form.getSearch()+"%");
					Predicate tbClass = cb.like(root.get("tbClass"), "%"+form.getSearch()+"%");
					Predicate curriculum = cb.like(root.get("curriculum"), "%"+form.getSearch()+"%");
					Predicate opplyTime = cb.like(root.get("opplyTime"), "%"+form.getSearch()+"%");
					Predicate or = cb.or(user,state,type,tbClass,curriculum,opplyTime);
					rules.add(or);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
			
		};
		return spec;
	}
	
	/**
	 * 日期调课
	 * */
	@RequestMapping("/g_ttb_rqtk")
	public String g_ttb_rqtk() {
		return "ttb/g_ttb_rqtk";
	}
	
	/**
	 * 日期调课
	 * 
	 * */
	@RequestMapping("/g_btn_rqtk")
	@ResponseBody
	public Object g_btn_rqtk(GRecordForm form) {
		List<FlowTable> list= flowTableService.findByTime(form.getSuspendTime());
		Integer str = 0;
		List<FlowTable> flow = new ArrayList<>();
		for (FlowTable kkk : list) {
			List<FlowTable> room = flowTableService.findByTimeAndJcAndClassRoom(form.getRepairTime(), kkk.getJc(), kkk.getClassRoom());		
			if(room.size() == 0){
				kkk.setTime(form.getRepairTime());
				String data = TimeUtils.WeekDay(form.getRepairTime());
				kkk.setWeek(data);
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				String date = df.format(new Date());
				form.setOpplyTime(date);
				flowTableService.save(kkk);				
			}else{
				str++;
				flow.add(kkk);
			}
		}
		return flow;
		/*if(str == 0){
			return new AjaxResult("调课成功！");			
		}else{
			return new AjaxResult("有 '"+str+" '门课有冲突！其他调课成功！");
		}*/
	}
	
	/**
	 * 日期停课
	 * */
	@RequestMapping("g_rqtk")
	public String g_rqtk() {
		return "ttb/g_rqtk";
	}
	
	@RequestMapping("/g_btn1_rqtk")
	@ResponseBody
	public Object g_btn1_rqtk(GRecordForm form) {
		List<FlowTable> list= flowTableService.findByTime(form.getSuspendTime());
		for (FlowTable f : list) {
			TtbRecord record = new TtbRecord();
			Teacher teacher = teacherService.findByName(f.getTeacher());
			SysUser sysUser = sysUserService.findById(teacher.getId());
			record.setTbClass(f.getClasses());
			record.setCurriculum(f.getCourse());
			record.setZxs(f.getZxs());
			record.setYdrs(f.getYdrs());
			record.setType("停课");
			record.setSuspendTime(f.getTime());
			record.setSection(f.getJc());
			record.setTypeTwo("已停课");
			record.setState("已同意");
			record.setKclbs(f.getKclbs());
			record.setUser(sysUser);
			record.setLc(f.getLc());
			record.setTeacher(teacher.getName());
			record.setLd(f.getLd());
			record.setYear(f.getYear());
			record.setTerm(f.getTerm());
			record.setXmh(f.getXmh());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String date = df.format(new Date());
			record.setOpplyTime(date);
			ttbService.save(record);
			flowTableService.delete(f);
		}
		return new AjaxResult("停课成功！");
	}
	/**
	 * 导出excel调停补记录表
	 * @return
	 */
	@RequestMapping(value = "/export")
	@ResponseBody
	public void export(String opplyTime,String opplyTime1, HttpServletRequest request, HttpServletResponse response)throws Exception{
		String op1 =opplyTime.substring(opplyTime.indexOf(",")+1);
		String op2 =opplyTime1.substring(opplyTime1.indexOf(",")+1);
		String title = "调停补课记录表("+op1+"——"+op2+")";
		String[] columnName = new String[] { "序号", "申请人", "申请时间", "班级", "课程", "调停补类型", "状态"};
		List<Object[]> dataList = new ArrayList<Object[]>();
		List<TtbRecord> rec = ttbService.findOpplyTime(op1,op2);
		for(TtbRecord r :rec){
			 SysUser user = r.getUser();
			Object[] objs = { user.getName(),user.getName(),r.getOpplyTime(),r.getTbClass(),r.getCurriculum(),r.getType(),r.getState()};
			dataList.add(objs);
		}
		ExportExcel ex = new ExportExcel(title, columnName, dataList, request, response);
		try {
			ex.export();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 调停补统计
	 * */
	@RequestMapping("/statistics")
	public String statistics() {
		return "ttb/statistics";
	}
	
	@RequestMapping("/ttb_tj")
	@ResponseBody
	public Object ttb_tj(TablePageable pageParam, GRecordForm form) {
		PageRequest pageable = pageParam.bulidPageRequest();
		Page<TtbRecord> page=ttbService.findBytypeCount(pageable);
		return AjaxResult.bulidPageResult(page);
	}
	
	@RequestMapping("/export1")
	@ResponseBody
	public void export1(String opplyTime,String opplyTime1, HttpServletRequest request, HttpServletResponse response)throws Exception{
		String op1 =opplyTime.substring(opplyTime.indexOf(",")+1);
		String op2 =opplyTime1.substring(opplyTime1.indexOf(",")+1);
		String title = "调停补课统计表("+op1+"——"+op2+")";
		String[] columnName = new String[] { "序号", "申请人","调停补类型", "课时"};
		List<Object[]> dataList = new ArrayList<Object[]>();
		List<TtbRecord> rec = ttbService.findBytypeCount1(op1,op2);
		for(TtbRecord r :rec){
			Object[] objs = {r.getUser().getName(), r.getUser().getName(), r.getType(), r.getXs()};
			dataList.add(objs);
		}
		ExportExcel ex = new ExportExcel(title, columnName, dataList, request, response);
		try {
			ex.export();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/***
	 * 班级停课 
	 */
	@RequestMapping("/tbclass_tk")
	public String tbclass_tk(ModelMap map) {
		List<String> list = professService.findAllYear();
		map.put("list", list);
		return "ttb/tbclass_tk";
	}
	
	@RequestMapping("/f_findfloor1")
	public Object f_findfloor1(String name,ModelMap map) {
		List<Profession> profession = professService.findByYear(name);
		map.put("profession", profession);
		return "ttb/g_change1";
	}
	
	@RequestMapping("/f_findfloor2")
	public Object f_findfloor2(String name, String year, ModelMap map) {
		Profession list = professService.findByyearAndprofesion(year, name);
		List<TbClass> tbClass = tbClassService.findByProfession_id(list.getId());
		map.put("tbClass", tbClass);
		return "ttb/g_change2";
	}
	
	/***
	 * 班级停课
	 * @param form
	 * @return
	 */
	@RequestMapping("/f_saveRecord1")
	@ResponseBody
	public Object f_saveRecord(GRecordForm form) {
		try {
			List<FlowTable> cr = flowTableService.findBybjsj(form.getTbClass(), form.getSuspendTime(), form.getRepairTime(), form.getYear());
			if(cr.size()==0) {
				return new AjaxResult(false, "此班级在该时间段没课，可重新选择！");
			}else {
				for (FlowTable ss : cr) {
					String str = ss.getClasses();
					String[] tbClass = str.split(",");
					TtbRecord record = new TtbRecord();
					record.setCurriculum(ss.getCourse());
					record.setClassroom(ss.getClassRoom());
					record.setSection(ss.getJc());
					record.setSuspendTime(ss.getTime());
					record.setKclbs(ss.getKclbs());
					record.setType("停课");
					record.setTypeTwo("已停课");
					record.setState("已同意");
					Teacher teacher = teacherService.findByName(ss.getTeacher());
					record.setUser(teacher);
					record.setTeacher(ss.getTeacher());
					record.setZxs(ss.getZxs());
					record.setYdrs(ss.getYdrs());
					record.setLc(ss.getLc());
					record.setLd(ss.getLd());
					record.setYear(ss.getYear());
					record.setTerm(ss.getTerm());
					record.setXmh(ss.getXmh());
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
					String date = df.format(new Date());
					record.setOpplyTime(date);
					if(tbClass.length == 1) {
						record.setTbClass(ss.getClasses());
						flowTableService.delete(ss);
					}else {
						record.setTbClass(form.getTbClass());
						String sum = "";
						for(Integer i = 0; i < tbClass.length; i++) {
							if(!tbClass[i].equals(form.getTbClass())) {
								sum += tbClass[i]+",";
							}
						}
						sum = sum.substring(0, sum.length()-1);
						ss.setClasses(sum);
						flowTableService.save(ss);
					}
					ttbService.save(record);
					
				}
			}
			return new AjaxResult("停课成功！");
		} catch (Exception e) {
			return new AjaxResult(false, "停课失败！");
		}
	}
}
